{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import cv2 as cv\n",
    "import numpy as np\n",
    "from matplotlib.pyplot import Rectangle\n",
    "from videofig import videofig\n",
    "\n",
    "sys.path.append('..')\n",
    "from pytracking.pysot_toolkit.datasets import DatasetFactory\n",
    "from pytracking.pysot_toolkit.environment import env_settings\n",
    "\n",
    "# set the dataset name here\n",
    "dataset_name = 'CVPR13'\n",
    "\n",
    "if dataset_name in ['CVPR13', 'OTB50', 'OTB100']:\n",
    "    # for OTB datasets, we save results into the same directory\n",
    "    save_dataset_name = 'OTB100'\n",
    "else:\n",
    "    save_dataset_name = dataset_name\n",
    "\n",
    "dataset_root = os.path.join(env_settings().dataset_path, save_dataset_name)\n",
    "\n",
    "# load dataset\n",
    "dataset = DatasetFactory.create_dataset(name=dataset_name, dataset_root=dataset_root, load_img=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset.videos.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Select results to show"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tracker_test_params = 'siamfc.default'\n",
    "exp_id = 'siamfc.siamfc_alexnet_vid.epoch49'\n",
    "videoname = 'Bolt'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Show"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib notebook\n",
    "\n",
    "if 'OTB100' == save_dataset_name:\n",
    "    filename = '{}.txt'.format(videoname)\n",
    "elif 'VOT' in save_dataset_name:\n",
    "    filename = 'baseline/{vname}/{vname}_001.txt'.format(vname=videoname)\n",
    "else:\n",
    "    raise NotImplemented\n",
    "    \n",
    "video = dataset[videoname]\n",
    "\n",
    "# load tracking results\n",
    "boxs = []\n",
    "with open(os.path.join(env_settings().results_path, save_dataset_name, tracker_test_params, exp_id, filename), 'r') as file_handle:\n",
    "    for line in file_handle:\n",
    "        boxs.append([float(v) for v in line.strip().split(',')])\n",
    "\n",
    "def redraw_fn(f, ax):\n",
    "    img_path, _  = video[f]\n",
    "    img = cv.cvtColor(cv.imread(img_path), cv.COLOR_BGR2RGB)\n",
    "    \n",
    "    box = boxs[f]\n",
    "    if len(box) == 4:\n",
    "        x, y, w, h = box\n",
    "    else:\n",
    "        x, y, w, h = 0, 0, 0, 0\n",
    "          \n",
    "    if not redraw_fn.initialized:\n",
    "        redraw_fn.img_handle = ax.imshow(img)\n",
    "        box_artist = Rectangle((x, y), w, h,\n",
    "                               fill=False,  # remove background\n",
    "                               lw=2,\n",
    "                               edgecolor=\"red\")\n",
    "        ax.add_patch(box_artist)\n",
    "        redraw_fn.box_handle = box_artist\n",
    "        redraw_fn.text_handle = ax.text(0., 1 - 0.05,\n",
    "                                        'Frame: {}'.format(f + 1),\n",
    "                                        transform=ax.transAxes,\n",
    "                                        color='yellow', size=12)\n",
    "        redraw_fn.initialized = True\n",
    "    else:\n",
    "        redraw_fn.img_handle.set_array(img)\n",
    "        redraw_fn.box_handle.set_xy((x, y))\n",
    "        redraw_fn.box_handle.set_width(w)\n",
    "        redraw_fn.box_handle.set_height(h)\n",
    "        redraw_fn.text_handle.set_text('Frame: {}'.format(f + 1))\n",
    "\n",
    "redraw_fn.initialized = False\n",
    "\n",
    "videofig(len(video), redraw_fn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
